#ifndef CONSOLE_HPP_
#define CONSOLE_HPP_

#include <string>
#include <list>

#include <SFML/Graphics.hpp>

#include "game/game.hpp"
#include "script.hpp"

namespace lge2d
{

class Console
{
public:
	Console();
	Console(sf::RenderWindow * window, lge2d::Script * script,
			sf::Font* font);

	void Initialize();
	void Update(float deltaTime);
	bool OnEvent(const sf::Event& event);
	void Render() const;
	void ExecuteLineOfScript(const std::string& utf8str);

	void SetScript(Script* script) {m_script = script;}
	void SetWindow(sf::RenderWindow* window) {m_window = window;}
	void SetFont(sf::Font* font) {m_font = font;}

	bool IsOnScreen() const {return m_on_screen;}

	virtual ~Console();
private:
	enum HistoryScrollDirection
	{
		HistscrollNone, HistScrollUp, HistScrollDown
	};

	bool OnTextEntered(const sf::Event& event);
	void OnKeyUp();
	void OnKeyDown();

	std::string GetLastNlinesOfResult(std::string& result);

	sf::Font* m_font;
	sf::RenderWindow* m_window;
	lge2d::Script* m_script;

	sf::String m_text;
	sf::String m_result_text;
	sf::String m_promt;
	sf::Shape m_console_background;
	std::list<std::string> m_last_input;
	HistoryScrollDirection m_history_scroll_dir;
	bool m_on_screen;

	void StoreLastInput();
	int GetMaxVisibleLines();
	void AppendChar(sf::Uint32 ch);
	void EraseLastChar();
};

} // namespace lge2d

#endif /* CONSOLE_HPP_ */
